<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Brand as brandModel;
use think\facade\Db;
use think\facade\View;

class Brand extends Base{
    // 列表
    public function index(){
    	$signid = input('signid'); $keyword = input('keyword'); $order = ['sequence'=>'desc','id'=>'desc'];

        // 赋值状态
        if(!is_numeric($signid)) $signid = 1;
        View::assign('signid',$signid);

		$rs = brandModel::index($signid,$this->lang,$keyword,$order);

		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('lastPage',$rs->lastPage());

        return view();
    }
	
    // 添加编辑
    public function edit($id=0){
        if(!request()->isPost()){
            $rs = brandModel::find($id);
            View::assign('rs',$rs);

            return view();
        }else{
			try {
                brandModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}

    // 单个更新状态
    public function update_signid(){
        $id = input('id'); $signid = input('signid');
        // 参数不能为空
        if(empty($id) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => array('id'=>$id,'signid'=>$signid)]);
        }
        // 软删除
        if($signid == 9) $signid = time();

        try {
            Db::name('brand')->where('id',$id)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    // 批量更新状态
    public function updateall_signid(){
        $selectid = input('selectid/a'); $signid = input('signid');
        // 参数错误
        if(empty($selectid) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid,'signid'=>$signid)]);
        }
        // 软删除
        if($signid == 9) $signid = time();

        try {
            Db::name('brand')->where('id','in',$selectid)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

	// 单条删除
    public function delete_one(){
        $id = input('id');
        $brand = brandModel::find($id);
        if(config('app.other.deletefile') == 1){// 删除对应的图片
		    $logo = substr($brand['logo'],1);
            @unlink($logo);
        }

		try {
            $brand->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	/* 批量删除，返回值：
    @ error:no_check 未勾选数据
    @ success 成功
    */
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }

		$deletefile = config('app.other.deletefile');
		foreach($selectid as $i => $rs){
		    $id = $selectid[$i];
			$brand = brandModel::find($id);
			if($deletefile == 1){// 删除对应的图片
			    $logo = substr($brand['logo'],1);
                @unlink($logo);
            }
	    }

		try {
            Db::name('brand')->delete($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }
	
	// 批量更新
    public function update_all(){
		$id = input('id/a'); $title = input('title/a'); $sequence = input('sequence/a');
        
		foreach($id as $i => $rs){
            $data[] = ['id'=>$id[$i],'title'=>$title[$i],'sequence'=>$sequence[$i]];
        }
        
        try {
            $brand = new brandModel;
            $brand->saveAll($data,true);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
}